<template>
  <div class="contentWrapper wpx1200" id="contentWrapper">
    <el-card class="box-card mt20">
		  <div slot="header" class="clearfix">
		    <span>设置-自定义词包</span>
		  </div>
		  <div>
		  	<div class="cardContentTop">
		  		 <el-form ref="form" label-width="88px">
					  <el-form-item label="词包名称:">
					    <el-input v-model="wordName" class="inputWrapper floatl mr20 w217" placeholder="请输入词包名称"></el-input> 
					  </el-form-item>
					  <el-form-item label="关键词:">
					    <textarea class="textarea" style="resize:none" v-model="wordTxt" placeholder="多个关键词请用空格隔开"></textarea>
					  </el-form-item>
					  <div class="mt50 mb30 pb40 clearFix">
							<div class="floatl">
								<el-button type="primary" size="small" @click="addKeyWord()">提交/修改</el-button>
								<router-link to="/setting">
									<el-button size="small">取消</el-button>
								</router-link>
							</div>
					  </div>
					</el-form>
		  	</div>
		  </div>
		</el-card>
  </div>
</template>

<script>
	import MsgBus from '@/components/bus.js';
	import API from "@/api"
	export default {
	  name: 'settingDetail',
	  data () {
	    return {
	    	type:'addword',
	    	wordName:'',
	    	wordTxt:'',
	    	wordId:'',
	    	createTime:'',
	    	createUser:'',
	    	wordType:''
	    }
	  },
	   
	  mounted: function () {
	  	this.checkData()
			setMinHeight("contentWrapper")
			$(window).resize(function() {
			  setMinHeight("contentWrapper")
			});
	  },
	  beforeDestroy: function(){
	  	sessionStorage.setItem("setdata",'')
	  },
	  methods: {
	  	checkData(){
	  		if(sessionStorage.getItem("setdata")){
		  		var data = JSON.parse(sessionStorage.getItem("setdata"))
					this.type = 'modifyword'
			   	this.wordName = data.wordName;
			   	this.wordTxt = data.wordTxt;
			   	this.wordId = data.wordId;
			   	this.createTime = data.createTime;
			   	this.createUser = data.createUser;
			   	this.wordType = data.wordType;
		  	}
	  	},
		  addKeyWord(){
		  	if(!this.wordName || !this.wordTxt){
	    			this.$message.error('词包名称或关键字不能为空');
		  	}else{
		  		if(this.type == 'modifyword'){
		  			var resetInfo = {
	    												info: {
	    													"wordId":this.wordId,
	    													"wordName":this.wordName,
	    													"wordTxt":this.wordTxt.ResetBlank(),
	    													"createTime":this.createTime,
	    													"createUser":this.createUser,
	    													"wordType":this.wordType,
	    												},
	    												token: API.header.getCookie("token"),
	    												url: this.type
	    											}
    				API.setting.addKeyWord(resetInfo).then(({data}) => {
				       if(data.code=='0'){
    							this.$message.success('修改成功');
    							this.wordName = '';
	    						this.wordTxt = '';
	    						this.$router.push({
							      name: 'setting',
							   })
				       }else{
    						this.$message.error(data.msg);
				       }
			      })
		  		}else{
		  			var addInfo = {
	    												info: {"wordName":this.wordName,"wordTxt":this.wordTxt.ResetBlank()},
	    												token: API.header.getCookie("token"),
	    												url: this.type
	    											}
    				API.setting.addKeyWord(addInfo).then(({data}) => {
				       if(data.code=='0'){
    							this.$message.success('新增成功');
    							this.wordName = '';
	    						this.wordTxt = '';
	    						this.$router.push({
							      name: 'setting',
							   })
				       }else{
    						this.$message.error(data.msg);
				       }
			      })
		  		}
	    			
		  	}
		  }
		}
	  
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	input {
		font-family: "Microsoft YaHei ","Hiragino Sans GB",Arial;
	}
	.cardContentTop {
		width: 500px;
    margin: 0 auto;
	}
	.textarea {
    width: 325px;
    border: 1px solid #dcdcdc;
    border-radius: 3px;
    height: 196px;
    padding: 20px;
    float: left;
    font-family: "Microsoft YaHei ","Hiragino Sans GB",Arial;
	}
	.w217 {
		width: 217px !important;
	}
</style>
